package com.university.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.university.mapper.MovieMapper;
import com.university.mapper.MovieTimeSlotMapper;
import com.university.pojo.cqe.command.CmdSaveMovie;
import com.university.pojo.cqe.command.CmdSaveTimeSlot;
import com.university.pojo.cqe.command.CmdUpdateMovie;
import com.university.pojo.cqe.command.CmdUpdateTimeSlot;
import com.university.pojo.cqe.query.MovieQuery;
import com.university.pojo.dto.MovieTimeSlotUserDTO;
import com.university.pojo.dto.MovieUserDTO;
import com.university.pojo.entity.Movie;
import com.university.pojo.entity.MovieTimeSlot;
import com.university.service.MovieService;
import com.university.utility.ObjectUtils;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Date: 2021/5/8 0:12
 * @Description:
 */
@Service
public class MovieServiceImpl implements MovieService {

    @Resource
    private MovieMapper movieMapper;
    @Resource
    private MovieTimeSlotMapper movieTimeSlotMapper;

    @Override
    public void saveMovie(CmdSaveMovie cmdSaveMovie) {
        Movie movie = ObjectUtils.convert(cmdSaveMovie, Movie.class);
        movieMapper.insert(movie);
    }

    @Override
    public void saveTimeSlot(CmdSaveTimeSlot cmdSaveTimeSlot) {
        MovieTimeSlot movieTimeSlot = ObjectUtils.convert(cmdSaveTimeSlot, MovieTimeSlot.class);
        movieTimeSlotMapper.insert(movieTimeSlot);
    }

    @Override
    @SneakyThrows
    public void updateMovie(CmdUpdateMovie cmdUpdateMovie) {
        Movie movie = ObjectUtils.convert(cmdUpdateMovie, Movie.class);
        int rows = movieMapper.updateById(movie);
        if (rows == 0) {
            throw new Exception("id is not exist");
        }
    }

    @Override
    @SneakyThrows
    public void updateTimeSlot(CmdUpdateTimeSlot cmdUpdateTimeSlot) {
        MovieTimeSlot movieTimeSlot = ObjectUtils.convert(cmdUpdateTimeSlot, MovieTimeSlot.class);
        int rows = movieTimeSlotMapper.updateById(movieTimeSlot);
        if (rows == 0) {
            throw new Exception("id is not exist");
        }
    }

    @Override
    @SneakyThrows
    public void deleteMovie(Integer id) {
        int rows = movieMapper.deleteMovie(id);
        if (rows == 0) {
            throw new Exception("id is not exist");
        }
    }

    @Override
    @SneakyThrows
    public void deleteTimeSlot(Integer id) {
        int rows = movieTimeSlotMapper.deleteById(id);
        if (rows == 0) {
            throw new Exception("id is not exist");
        }
    }

    @Override
    public IPage<Movie> adminGetMovie(MovieQuery query) {
        IPage<Movie> page = new Page<>(query.getPage(), query.getPageSize());
        return movieMapper.adminGetMoviePage(page, query);
    }

    @Override
    public List<MovieTimeSlot> adminGetTimeSlot(Integer movieId) {
        QueryWrapper<MovieTimeSlot> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MovieTimeSlot::getMovieId, movieId);
        return movieTimeSlotMapper.selectList(wrapper);
    }

    @Override
    public List<MovieUserDTO> userGetMovie() {
        return movieMapper.userGetMovie();
    }

    @Override
    public List<MovieTimeSlotUserDTO> userGetTimeSlot(Integer movieId) {
        return movieMapper.userGetTimeSlot(movieId);
    }

}
